/**
 * 
 */
package com.kingcore.director.setting.ui;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import wzw.beans.DbBean;
import wzw.sql.DatabaseOperator;
import wzw.util.ComponentUtil;

import com.kingcore.director.DirectorConstants;
import com.kingcore.framework.context.ApplicationContext;
import com.sunking.swing.JIpAddressField;

/** 
 * @author WZWEN
 *
 */
public class NetConfigPanel extends JPanel implements DatabaseOperator{

	private JPanel pmail = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTextField proxyIP = null;
	private JTextField proxyUser = null;
	private JTextField proxyPort = null;
	private JTextField proxyPass = null;
	private JLabel jLabel4 = null;
	private JComboBox proxyType = null;
	private JButton btnTest = null;
	private Logger log = Logger.getLogger(NetConfigPanel.class);

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPmail() {
		if (pmail == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new java.awt.Rectangle(19,19,66,23));
			jLabel4.setText("代理类型：");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new java.awt.Rectangle(265,77,47,22));
			jLabel3.setText("密码：");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new java.awt.Rectangle(265,50,47,22));
			jLabel2.setText("端口：");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new java.awt.Rectangle(19,77,59,22));
			jLabel1.setText("用户ID：");
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(19,50,59,23));
			jLabel.setText("服务器：");
			pmail = new JPanel();
			pmail.setLayout(null);
			pmail.setBounds(new java.awt.Rectangle(20,18,422,127));
			pmail.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "邮件发送代理", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
			pmail.add(jLabel, null);
			pmail.add(jLabel1, null);
			pmail.add(jLabel2, null);
			pmail.add(jLabel3, null);
			pmail.add(getProxyIP(), null);
			pmail.add(getProxyUser(), null);
			pmail.add(getProxyPort(), null);
			pmail.add(getProxyPass(), null);
			pmail.add(jLabel4, null);
			pmail.add(getProxyType(), null);
			pmail.add(getBtnTest(), null);
		}
		return pmail;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getProxyIP() {
		if (proxyIP == null) {
			proxyIP = new JIpAddressField();	//JTextField
			proxyIP.setBounds(new java.awt.Rectangle(88,50,172,23));
		}
		return proxyIP;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getProxyUser() {
		if (proxyUser == null) {
			proxyUser = new JTextField();
			proxyUser.setBounds(new java.awt.Rectangle(88,77,172,23));
		}
		return proxyUser;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getProxyPort() {
		if (proxyPort == null) {
			proxyPort = new JTextField();
			proxyPort.setBounds(new java.awt.Rectangle(316,50,88,23));
		}
		return proxyPort;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getProxyPass() {
		if (proxyPass == null) {
			proxyPass = new JTextField();
			proxyPass.setBounds(new java.awt.Rectangle(316,77,88,23));
		}
		return proxyPass;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getProxyType() {
		if (proxyType == null) {
			proxyType = new JComboBox();
			proxyType.setBounds(new java.awt.Rectangle(96,16,110,23));
			ComponentUtil.initComboBox( proxyType, new String[][]{{"socktProxy","socket 5"},{"httpProxy","http 代理"}});
		}
		return proxyType;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnTest() {
		if (btnTest == null) {
			btnTest = new JButton();
			btnTest.setBounds(new java.awt.Rectangle(290,12,95,29));
			btnTest.setText("测试(C)");
		}
		return btnTest;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * This is the default constructor
	 */
	public NetConfigPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(null);
		this.setSize(476, 395);
		this.add(getPmail(), null);
		try {
			this.fetchData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean saveData() throws SQLException {
		DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
		try {
			List<String> l_sql=new ArrayList<String>();
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+ ComponentUtil.getSelectedItemCode( getProxyType() ) + "' " +
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyTppe+"'" );
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+getProxyIP().getText() + "' " +
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyIp+"'" );
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+getProxyPort().getText() + "' " +
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyPort+"'" );
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+getProxyUser().getText() + "' " +
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyUser+"'" );
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+getProxyPass().getText() + "' " +
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyPass+"'" );
			dbBean.executeBatch(l_sql);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	public boolean fetchData() throws SQLException {

		String sql="";
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt=conn.createStatement();
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting+" "+
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyTppe+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				ComponentUtil.setSelectedItemByCode( getProxyType(), rs.getString("VAL"));
			}
			
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting+" "+
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyIp+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				((JIpAddressField)(getProxyIP())).setIpAddress( rs.getString("VAL"));
			}
			
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting+" "+
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyPort+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				getProxyPort().setText( rs.getString("VAL"));
			}
			
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting+" "+
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyUser+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				getProxyUser().setText( rs.getString("VAL"));
			}
			
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting+" "+
					"Where KEY='"+DirectorConstants.Net_Mail_ProxyPass+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				getProxyPass().setText( rs.getString("VAL"));
			}

		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,e1.getMessage(),"查询失败",JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();						
		} finally{
			//释放资源
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return true;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
